package com.cabrida.livestar.main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.model.FansAdvanceListData;
import com.cabrida.livestar.util.MyUtils;

/**
 * 首页预告adapter
 * 
 * @author why
 * 
 */
public class FansHomePageAdvanceAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater mInflater;
	private ArrayList<FansAdvanceListData> data_list = new ArrayList<FansAdvanceListData>();

	public FansHomePageAdvanceAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public void setData(ArrayList<FansAdvanceListData> data) {
		data_list = data;
	}
	
	public ArrayList<FansAdvanceListData> getData(){
		return data_list;
	}

	@Override
	public int getCount() {
		return data_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(
					R.layout.adapter_homepage_lv_notice_item, null);
			convertView.setTag(holder);
			holder.iv_image = (ImageView) convertView
					.findViewById(R.id.iv_image);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_advance = (TextView) convertView
					.findViewById(R.id.tv_advance);
			holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_advance_number = (TextView) convertView.findViewById(R.id.tv_advance_number);
			holder.tv_gift = (TextView) convertView.findViewById(R.id.tv_gift);
			holder.tv_package = (TextView) convertView.findViewById(R.id.tv_package);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		 MyUtils.loadImg(data_list.get(position).getS_image(), holder.iv_image);
		holder.tv_name.setText(data_list.get(position).getS_name().toString());
		holder.tv_date.setText(data_list.get(position).getL_date().toString());
		holder.tv_time.setText(data_list.get(position).getL_startime().toString() + "开播");
		holder.tv_advance_number.setText(data_list.get(position).getL_booked()+"人预选");
		
		String str_gift  = data_list.get(position).getL_gift().toString();
		if (TextUtils.isEmpty(str_gift)) {
			holder.tv_gift.setVisibility(View.INVISIBLE);
		}else {
			int int_gift = Integer.valueOf(str_gift);
			if (int_gift>0) {
				holder.tv_gift.setVisibility(View.VISIBLE);
			}else {
				holder.tv_gift.setVisibility(View.INVISIBLE);
			}
		}
		
		String str_package  = data_list.get(position).getL_package().toString();
		if (TextUtils.isEmpty(str_package)) {
			holder.tv_package.setVisibility(View.INVISIBLE);
		}else {
			int int_package = Integer.valueOf(str_package);
			if (int_package>0) {
				holder.tv_package.setVisibility(View.VISIBLE);
			}else {
				holder.tv_package.setVisibility(View.INVISIBLE);
			}
		}

		if (data_list.get(position).getIsCheck() == 0) {
			holder.tv_advance.setText("+订阅");
			holder.tv_advance.setTextColor(context.getResources().getColor(R.color.white));
			holder.tv_advance.setBackgroundResource(R.drawable.bg_green);
		} else {
			holder.tv_advance.setText("已订阅");
			holder.tv_advance.setTextColor(context.getResources().getColor(R.color.green));
			holder.tv_advance.setBackgroundResource(R.drawable.bg_green_line_white);
		}

		holder.tv_advance.setTag(position);
		holder.tv_advance.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int pos = (Integer) v.getTag();
//				if (data_list.get(pos).getIsCheck() == 0) {
//					data_list.get(pos).setIsCheck(1);
//				} else {
//					data_list.get(pos).setIsCheck(0);
//				}
//				notifyDataSetChanged();
				Intent intent = new Intent();
				intent.setAction(MainActivity.ACTION_ADVANCE_REFRESH);
				intent.putExtra("follow", data_list.get(pos).getL_id());
				context.sendBroadcast(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {
		private ImageView iv_image;
		private TextView tv_name;
		private TextView tv_advance;
		private TextView tv_advance_number;
		private TextView tv_date;
		private TextView tv_time;
		private TextView tv_gift;
		private TextView tv_package;
	}

}
